//
//  XHDiscoverViewController.m
//  testApp
//
//  Created by ColdMountain on 2016/12/1.
//  Copyright © 2016年 ColdMountain. All rights reserved.
//

#import "XHDiscoverViewController.h"
#import "ActivityViewController.h"

#import "Services.h"

#import "XHPlayerCell.h"

#import <testApp-Swift.h>
#import <UINavigationController+FDFullscreenPopGesture.h>
#import <SVPullToRefresh/SVPullToRefresh.h>
#import <MJRefresh.h>

static const NSUInteger kPageSize = 20;
static const NSUInteger kPageInit = 0;

@interface XHDiscoverViewController ()<XHPlayerCellDelegate>
@property (weak, nonatomic) IBOutlet UITableView *ui_tableView;
@property (strong, nonatomic) NSMutableArray *m_messagesArray;
@property (assign, nonatomic) NSUInteger page;
@property (assign, nonatomic) BOOL hasNextPage;
@end

@implementation XHDiscoverViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    
    [PublicMethod setExtraCellLineHidden:self.ui_tableView];
    [self.ui_tableView registerNib:[UINib nibWithNibName:@"XHPlayerCell" bundle:nil] forCellReuseIdentifier:XHPlayerCellIdentifier];
    self.page=kPageInit;
    self.ui_tableView.mj_header=[MJRefreshNormalHeader headerWithRefreshingBlock:^{
        if (!self.hasNextPage)
        {
        }
        if (self.m_messagesArray.count>=kPageSize){
            [self postWithPage:self.page+1];
            return;
        }
        [self.ui_tableView.mj_header endRefreshing];
    }];
    [self.ui_tableView.mj_header beginRefreshing];
    
    self.ui_tableView.mj_footer = [MJRefreshAutoNormalFooter footerWithRefreshingBlock: ^ {
        //调用此块自动进入刷新状态
        self.page=kPageInit;
        [self postWithPage:self.page];
    }];
}


- (void)viewWillAppear:(BOOL)animated
{
    [super viewWillAppear:animated];
//    [self.navigationController setNavigationBarHidden:YES animated:animated];
    self.navigationController.fd_prefersNavigationBarHidden = YES;
    if (!self.m_messagesArray)
    {
        self.page=kPageInit;
        [self postWithPage:self.page];
    }
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}


- (void)postWithPage:(NSInteger)page
{
    [ServiceManager postActivityWithPageIndex:page success:^(NSDictionary *data) {
        if (page==kPageInit||self.m_messagesArray==nil)
        {
            self.m_messagesArray=[NSMutableArray array];
        }
        self.hasNextPage=(data.count==kPageSize);
        if (data.count>0)
        {
            self.page=page;
            [self.m_messagesArray addObjectsFromArray:data];
        }
        NSLog(@"数组有%ld个",self.m_messagesArray.count);
        [self.ui_tableView reloadData];
        [self.ui_tableView.mj_header endRefreshing];
        [self.ui_tableView.mj_footer endRefreshing];
    } failure:^(NSDictionary *data) {
        [self.ui_tableView.mj_header endRefreshing];
        [self.ui_tableView.mj_footer endRefreshing];
    }];
}


#pragma mark - UITableViewDataSource

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    return 1;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    NSUInteger rows=self.m_messagesArray.count;
    return rows;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    XHPlayerCell * cell = [self.ui_tableView dequeueReusableCellWithIdentifier:XHPlayerCellIdentifier];
    cell.delegate=self;
    NSDictionary * dic = self.m_messagesArray[indexPath.row];
    cell.title.text = [NSString stringWithFormat:@"%@",dic[@"bigtype"]];
    cell.content.text = [NSString stringWithFormat:@"%@",dic[@"title"]];
    cell.ui_time.text = [NSString stringWithFormat:@"%@",dic[@"publishtime"]];
    
    return cell;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    [tableView deselectRowAtIndexPath:indexPath animated:YES];
    NSDictionary * dic = self.m_messagesArray[indexPath.row];
//    XHActivityViewController * viewController = [[XHActivityViewController alloc]init];
//    viewController.title=@"资讯详情";
//    viewController.ID=dic[@"id"];
//    viewController.hidesBottomBarWhenPushed=YES;
//    [self.navigationController pushViewController:viewController animated:YES];
    ActivityViewController * aViewController = [[ActivityViewController alloc]init];
    aViewController.title=@"资讯详情";
    aViewController.ID=dic[@"id"];
    aViewController.hidesBottomBarWhenPushed=YES;
    [self.navigationController pushViewController:aViewController animated:YES];

}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    CGFloat height=0;
    XHPlayerCell*cell=(XHPlayerCell*)[self tableView:self.ui_tableView cellForRowAtIndexPath:indexPath];
    height=[cell getImageSizeHeight];
    return height;
}

#pragma - mark XHPlayerCellDelegate

- (void)runAction:(UIButton *)button
{
    
}


/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
